package First;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class RegisterPage extends JFrame {
    private JTextField idField;
    private JPasswordField passwordField;
    private JTextField nameField;
    private JTextField classField;
    private JTextField ageField;
    private JTextField majorField;
    private JButton registerButton;

    public RegisterPage() {
        createView();

        setTitle("学生管理系统 - 注册页面");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 400);
        setLocationRelativeTo(null);
        setResizable(false);
    }

    private void createView() {
        JPanel panel = new JPanel(new GridBagLayout());
        getContentPane().add(panel);

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(10, 10, 10, 10);

        // Id
        JLabel idLabel = new JLabel("学号: ");
        constraints.gridx = 0;
        constraints.gridy = 0;
        panel.add(idLabel, constraints);

        idField = new JTextField(20);
        constraints.gridx = 1;
        panel.add(idField, constraints);

        // Password
        JLabel passwordLabel = new JLabel("密码: ");
        constraints.gridx = 0;
        constraints.gridy = 1;
        panel.add(passwordLabel, constraints);

        passwordField = new JPasswordField(20);
        constraints.gridx = 1;
        panel.add(passwordField, constraints);

        // Name
        JLabel nameLabel = new JLabel("姓名: ");
        constraints.gridx = 0;
        constraints.gridy = 2;
        panel.add(nameLabel, constraints);

        nameField = new JTextField(20);
        constraints.gridx = 1;
        panel.add(nameField, constraints);

        // Class
        JLabel classLabel = new JLabel("班级: ");
        constraints.gridx = 0;
        constraints.gridy = 3;
        panel.add(classLabel, constraints);

        classField = new JTextField(20);
        constraints.gridx = 1;
        panel.add(classField, constraints);

        // Age
        JLabel ageLabel = new JLabel("年龄: ");
        constraints.gridx = 0;
        constraints.gridy = 4;
        panel.add(ageLabel, constraints);

        ageField = new JTextField(20);
        constraints.gridx = 1;
        panel.add(ageField, constraints);

        // Major
        JLabel majorLabel = new JLabel("专业: ");
        constraints.gridx = 0;
        constraints.gridy = 5;
        panel.add(majorLabel, constraints);

        majorField = new JTextField(20);
        constraints.gridx = 1;
        panel.add(majorField, constraints);

        // Register button
        registerButton = new JButton("确认注册");
        constraints.gridx = 0;
        constraints.gridy = 6;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.CENTER;
        panel.add(registerButton, constraints);

        registerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Register logic goes here
                String id = idField.getText();
                String password = new String(passwordField.getPassword());
                String name = nameField.getText();
                String classNum = classField.getText();
                String age = ageField.getText();
                String major = majorField.getText();

                Student student = new Student(id, password, name, classNum, age, major);

                File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "students.txt");
                try {
                    if(!file.exists()) {
                        file.createNewFile();
                    }

                    FileWriter writer = new FileWriter(file, true);
                    writer.write(student.toString() + "\n");
                    writer.close();

                    JOptionPane.showMessageDialog(
                            RegisterPage.this,
                            "恭喜你，注册成功!",
                            "成功",
                            JOptionPane.INFORMATION_MESSAGE
                    );
                    new LoginPage().setVisible(true);
                    RegisterPage.this.dispose();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}
